package com.rxyb.hongyu.service.logic;

import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.gateway.dto.*;
import com.rxyb.hongyu.gateway.dto.pull.*;
import com.rxyb.hongyu.gateway.enums.RspCodeForHongyu;
import com.rxyb.toolize.VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 报文校验
 *
 * @author duyh
 * @date 2020/6/9
 */
@Service
public class HongyuPullValidateLogic {
    
    @Autowired
    private HongyuDictionaryLogic hongyuDictionaryLogic;

    public HongYuRsp validate(F20C01Req req) {
        HongYuRsp rsp = new HongYuRsp();
        // 报文校验
        if (null == req) {
            rsp.setRetcode(RspCodeForHongyu.NO_DOC.getCode());
            rsp.setRetmessage(RspCodeForHongyu.NO_DOC.getDesc());
            return rsp;
        }
        // 验证鸿宇参数字典是否正确
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_HPZL, req.getHpzl()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getHpzl()));
            return rsp;
        }
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_LSZT, req.getLszt()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getLszt()));
            return rsp;
        }
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C02Req req) {
        HongYuRsp rsp = new HongYuRsp();
        // 验证鸿宇参数字典是否正确
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_HPZL, req.getHpzl()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getHpzl()));
            return rsp;
        }
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_CLLX, req.getCllx()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getCllx()));
            return rsp;
        }
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_SYXZ, req.getSyxz()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getSyxz()));
            return rsp;
        }
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C03Req req) {
        HongYuRsp rsp = new HongYuRsp();
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_HPZL, req.getHpzl()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getHpzl()));
            return rsp;
        }
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C04Req req) {
        // 实况图没有以下参数，补充校验
        req.setHpzl("00");
        req.setHphm("000000");
        req.setJylsh("00000000000000000");
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C05Req req) {
        HongYuRsp rsp = new HongYuRsp();
        if (VO.isBlank(hongyuDictionaryLogic.query(HongYuConstants.DMLB_ZPZL, req.getZpzl()))) {
            rsp.setRetcode(RspCodeForHongyu.UNKNOWN_KEY.getCode());
            rsp.setRetmessage(RspCodeForHongyu.UNKNOWN_KEY.getDesc().replace("${value}", req.getZpzl()));
            return rsp;
        }
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C06Req req) {
        HongYuRsp rsp = new HongYuRsp();
        if (HongYuConstants.UN_PASS.equals(req.getShjg()) && VO.isBlank(req.getXqms())) {
            rsp.setRetcode(RspCodeForHongyu.FAILED.getCode());
            rsp.setRetmessage("详情描述[xqms]不能为空");
            return rsp;
        }
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C07Req req) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public HongYuRsp validate(F20C08Req req) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        return rsp;
    }

    public String getApiKey(F20C01Req info) {
        // 报告：流水号+检验次数
        return info.getJylsh() + info.getLszt();
    }

    public String getApiKey(F20C02Req info) {
        // 报告：流水号+检验次数
        return info.getJylsh() + info.getJycs();
    }

    public String getApiKey(F20C03Req info) {
        // 流水号
        return info.getJylsh();
    }

    public String getApiKey(F20C05Req info) {
        // 流水号+ 审核次数+检验次数+照片种类
        return info.getJylsh() + info.getShcs() + info.getJycs() + info.getZpzl();
    }

    public String getApiKey(F20C06Req info) {
        // 流水号+审核次数
        return info.getJylsh() + info.getShcs();
    }
}

